
#include "c3_main.h"
#include "zhongduan.h"
#include "gatts_table_creat_demo.h" //线程缓冲区消息

#include <inttypes.h>
#include <string.h>
#include "zhi_xing.h"

static uint16_t kaiShiShi = 0;      // 中断启用开始时间(时)
static uint16_t jieShuShi = 23;     // 中断启用结束时间(时)
static uint16_t kaiShiFen = 0;      // 中断启用开始时间(分)
static uint16_t jieShuFen = 59;     // 中断启用结束时间(分)
static bool shiJianTongBuB = false; // 时间同步标志

static const char *TAG = "zhongduan: ";
#define GPIO_INPUT_PIN 7

#define GPIO_OUTPUT_IO_0 18
#define GPIO_OUTPUT_IO_1 19
#define GPIO_OUTPUT_PIN_SEL ((1ULL << GPIO_OUTPUT_IO_0) | (1ULL << GPIO_OUTPUT_IO_1))
/*
 * Let's say, GPIO_OUTPUT_IO_0=18, GPIO_OUTPUT_IO_1=19
 * In binary representation,
 * 1ULL<<GPIO_OUTPUT_IO_0 is equal to 0000000000000000000001000000000000000000 and
 * 1ULL<<GPIO_OUTPUT_IO_1 is equal to 0000000000000000000010000000000000000000
 * GPIO_OUTPUT_PIN_SEL                0000000000000000000011000000000000000000
 * */
#define GPIO_INPUT_IO_0 4
#define GPIO_INPUT_IO_1 5
#define GPIO_INPUT_PIN_SEL ((1ULL << GPIO_INPUT_IO_0) | (1ULL << GPIO_INPUT_IO_1))
/*
 * Let's say, GPIO_INPUT_IO_0=4, GPIO_INPUT_IO_1=5
 * In binary representation,
 * 1ULL<<GPIO_INPUT_IO_0 is equal to 0000000000000000000000000000000000010000 and
 * 1ULL<<GPIO_INPUT_IO_1 is equal to 0000000000000000000000000000000000100000
 * GPIO_INPUT_PIN_SEL                0000000000000000000000000000000000110000
 * */
#define ESP_INTR_FLAG_DEFAULT 0
#define DEBOUNCE_TIME_MS 200 // 设置去抖时间
static volatile TickType_t last_interrupt_time = 0;

static QueueHandle_t gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void *arg)
{
    // uint32_t gpio_num = (uint32_t)arg;//传递发生中断的IO口
    TickType_t now = xTaskGetTickCountFromISR(); // 获取当前 tick count
    if ((now - last_interrupt_time) * portTICK_PERIOD_MS > DEBOUNCE_TIME_MS)
    {
        // 通过队列发送消息到任务处理中断
        bool ppdd = true;
        xQueueSendFromISR(gpio_evt_queue, &ppdd, NULL);
        last_interrupt_time = now; // 更新最后中断时间
    }
}
static void gpio_task_example(void *arg)
{
    get_shi_jian_a();
    size_t xBytesSent;
    //char *pcStringToSend = "{\"lx\":1}"; // unsigned char数据转换char
    const TickType_t x100ms = pdMS_TO_TICKS(100);
    bool pd = false;
    uint16_t lx =1;
    for (;;)
    {
        if (xQueueReceive(gpio_evt_queue, &pd, portMAX_DELAY))
        {
            if (pd)
            {
                if (!shiJianTongBuB)
                {
                    if (xQueueSend(ledPwmLD, &lx, portMAX_DELAY) != pdPASS)
                    {
                        pd = false;
                    }
                }
                else if (shi_jian_dui_bi_shi(kaiShiShi, jieShuShi, kaiShiFen, jieShuFen) == 0)
                {
                    if (xQueueSend(ledPwmLD, &lx, portMAX_DELAY) != pdPASS)
                    {
                        pd = false;
                    }
                    //xBytesSent = xMessageBufferSend(zhuanFanP, (void *)pcStringToSend, strlen(pcStringToSend), x100ms);
                    //vTaskDelay(400 / portTICK_PERIOD_MS);
                    //pd = false;
                }
            }
        }
        vTaskDelay(200 / portTICK_PERIOD_MS);
    }
}

// 中断初始化,参数1:IO口编号
void zhong_duan_init()
{
    // 对配置结构进行零初始化.
    gpio_config_t io_conf = {};
    // 上升沿中断
    io_conf.intr_type = GPIO_INTR_ANYEDGE;
    // 引脚的位掩码，此处使用 GPIO4/5
    io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
    // 设置为输入模式
    io_conf.mode = GPIO_MODE_INPUT;
    // 使能上拉模式
    // io_conf.pull_up_en = 1;
    io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
    gpio_config(&io_conf);

    // 更改一个引脚的 GPIO 中断类型
    // gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);

    // 创建一个队列来处理来自 isr 的 gpio 事件
    gpio_evt_queue = xQueueCreate(10, sizeof(uint8_t));
    // 启动GPIO任务
    xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);

    // 安装gpio isr服务
    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
    // 针对特定 GPIO 引脚的钩子 isr 处理程序
    gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *)GPIO_INPUT_IO_0);
    // 针对特定 GPIO 引脚的钩子 isr 处理程序
    gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void *)GPIO_INPUT_IO_1);

    // 删除 GPIO 编号的 isr 处理程序.
    // gpio_isr_handler_remove(GPIO_INPUT_IO_0);
    // 再次挂钩特定 GPIO 引脚的 isr 处理程序
    // gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *)GPIO_INPUT_IO_0);
}

void get_shi_jian_a()
{
    nvs_handle_t my_handle;
    esp_err_t err;
    // Open
    err = nvs_open(ZIDINGYI_NVS_KONG_JIAN, // 命名空间名称
                   NVS_READWRITE,          // NVS_READWRITE 或 NVS_READONLY。如果 NVS_READONLY，将打开一个只读句柄。
                   &my_handle);            // 如果成功（返回代码为零），句柄将在此参数中返回。
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "err ==  1\n");
        nvs_close(my_handle);
        return;
    }
    /*这个是获取要获取int16_t*/
    err = nvs_get_u16(my_handle, "kaiShiShi", &kaiShiShi);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "kaiShiShi ==  != ESP_OK\n");
        kaiShiShi = 0;
    }
    err = nvs_get_u16(my_handle, "jieShuShi", &jieShuShi);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "jieShuShi ==  != ESP_OK\n");
        jieShuShi = 23;
    }
    err = nvs_get_u16(my_handle, "kaiShiFen", &kaiShiFen);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "kaiShiFen ==  != ESP_OK\n");
        kaiShiFen = 0;
    }
    err = nvs_get_u16(my_handle, "jieShuFen", &jieShuFen);
    if (err != ESP_OK)
    {
        ESP_LOGI(TAG, "jieShuFen ==  != ESP_OK\n");
        jieShuFen = 59;
    }
    nvs_close(my_handle);
}

void set_shi_jian_tong_bu()
{
    shiJianTongBuB = true;
}

